package com.xiaochong.tir.manager.controller;

import com.github.pagehelper.PageInfo;
import com.xiaochong.service.common.config.redis.RedisServer;
import com.xiaochong.tir.common.data.vo.BtUser;
import com.xiaochong.tir.common.data.vo.BtUserVo;
import com.xiaochong.tir.common.util.RedisKeysUtils;
import com.xiaochong.tir.manager.service.BtUserInfoService;
import com.xiaochong.tir.manager.service.BtUserService;
import com.xiaochong.tir.manager.util.ManagerVersion;
import com.xiaochong.tir.manager.util.ReturnResult;
import com.xiaochong.tir.manager.util.SessionUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: WangXing
 * @Description:
 * @CreateDate: 2018/8/2 10:53
 */
@RestController
@RequestMapping(value = ManagerVersion.API_V1_PREFIX+"member/")
@Api("会员管理")
public class MemberController {

    @Autowired
    private BtUserService btUserService;
    @Autowired
    private RedisServer redisServer;
    /**
     * 会员管理页面
     * @return
     */
    @GetMapping("mempage")
    public Object memberPage(){
        return new ModelAndView("member/memberpage");
    }

    /**
     * 查询会员列表
     * @param vo
     * @param pageSize
     * @param pageIndex
     * @return
     */
    @ApiOperation(value = "查询会员列表",notes = "查询会员列表")
    @PostMapping("memList")
    public Object getMemberList(BtUserVo vo,
                                @RequestParam("limit") Integer pageSize,
                                @RequestParam("page")Integer pageIndex){
        PageInfo<BtUser> list = btUserService.selectByBtUser(vo,pageSize,pageIndex);
        ReturnResult<BtUser> result = new ReturnResult<>(0,"成功",list.getList(),list.getTotal());
        return result;
    }

    /**
     * 修改会员状态
     * @param btUser
     * @return
     */
    @ApiOperation(value = "修改会员状态",notes = "修改会员状态")
    @PostMapping("enableMen")
    public Object updateStruts(BtUser btUser){
        redisServer.set(RedisKeysUtils.LOGINNAME,"修改会员状态");
        if(btUser != null){
            BtUser old = btUserService.selectByEntity(btUser);
            if(btUser.getStatus().equals(1)){
                btUser.setStatus(0);
            }else {
                btUser.setStatus(1);
            }
            btUserService.updateBybtUserKey(btUser,old);
        }
        return new ReturnResult<>(0,"成功",0,0);
    }

    /**
     * 查看会员
     * @param id
     * @return
     */
    @GetMapping("userdetil")
    public Object userDetil(@RequestParam("id") Integer id){
        BtUser btUser = btUserService.selectByPrimaryKey(id);
        ModelAndView mv = new ModelAndView("member/viewuser");
        mv.addObject("user",btUser);
        return mv;
    }

    /**
     * 查询来源列表
     * @return
     */
    @ApiOperation(value = "查询会员来源",notes = "查询会员来源")
    @GetMapping("source")
    public Object gerSource(){
      List<BtUser> list = btUserService.selectByEntityList(new BtUser());
      List<String> result = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            BtUser user = list.get(i);
            if(!result.contains(user.getSource())){
                result.add(user.getSource());
            }
        }
        return result;
    }

}
